package com.zyk.scaffold.oauth.component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 保存到缓存中
 */
@Component
public class TenantCodeCachComponent {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    public static final String TENANT_CODE_CACHE_KEY = "tenantcode:";

    @Autowired
    private StringRedisTemplate redisTemplate;

    public boolean saveTenantCode(String account, String tenantCode) {
        account = covertAccount(account);
        redisTemplate.opsForValue().set(TENANT_CODE_CACHE_KEY + account, tenantCode);
        return true;
    }

    public String getTenantCode(String account) {
        account = covertAccount(account);
        String captcha = redisTemplate.opsForValue().get(TENANT_CODE_CACHE_KEY + account);
        return captcha;
    }

    private String covertAccount(String account) {
        if(account == null){
            return null;
        }
        return account.toLowerCase();
    }

    public void removeTenantCode(String account) {
        redisTemplate.delete(TENANT_CODE_CACHE_KEY + account);
    }
}
